Texture2D diffuseTexture	: register(t0);
Texture2D normalTexture		: register(t1);

SamplerState defaultSampler : register(s0);

struct P_IN
{
	float2 texCoord		: TEXCOORD;
	float4 actualPos	: POSITION;
	float4 pos			: SV_POSITION;
};

cbuffer color : register(b0)
{
	float4 color;
};

cbuffer texturesUsed : register(b1)
{
	//x == DIFFUSE
	//Y == NORMAL
	//Z == SPECULAR
	float4 texturesUsed;
};

cbuffer lightInformation : register(b2)
{
	float4 lightPosition;
	float4 lightColor;
};

float4 PS(P_IN input) : SV_TARGET
{
	float4 diffuseColor = float4(0.0f, 0.0f, 0.0f, 0.0f);
	float4 normalColor = float4(0.0f, 0.0f, 0.0f, 0.0f);

	//Sample Diffuse
	if(texturesUsed.x)
	{
		diffuseColor = diffuseTexture.Sample(defaultSampler, input.texCoord);
	}

	//Sample Normal
	if(texturesUsed.y)
	{
		normalColor = normalTexture.Sample(defaultSampler, input.texCoord);
	}

	float4 finalColor;
	if(normalColor.z > .95f && normalColor.z < 1.05f)
	{
		finalColor = diffuseColor;
	}
	else
 	{
		normalColor.x = (normalColor.x * 2.0f) - 1.0f;
		normalColor.y = (normalColor.y * 2.0f) - 1.0f;
		normalColor.z = (normalColor.z * 2.0f) - 1.0f;

		normalColor = normalize(normalColor);
		normalColor.z = -normalColor.z;

 		float3 lightDirection = input.actualPos.xyz - lightPosition.xyz;
 
		lightDirection = normalize(lightDirection);
 		float lightIntensity = dot(-lightDirection, normalColor.xyz);
 
		float4 light = float4(0.0f, 0.0f, 0.0f, 0.0f);

		if(lightIntensity > 0)
 			light = lightColor * lightIntensity;
 
		finalColor = diffuseColor;
		finalColor.xyz += light.xyz;
	}

	return finalColor;

	//return (diffuseColor * color);
}